﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.AP.Payment;
	using UFIDA.U9.CBO.FI.Enums;
	using UFIDA.U9.CBO.Pub.Controller;
	using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

	/// <summary>
	/// ConfirmReqFundRevocateSV partial 
	/// </summary>	
	public partial class ConfirmReqFundRevocateSV
	{	
		internal BaseStrategy Select()
		{
			return new ConfirmReqFundRevocateSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class ConfirmReqFundRevocateSVImpementStrategy : BaseStrategy
	{
		public ConfirmReqFundRevocateSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			ConfirmReqFundRevocateSV bpObj = (ConfirmReqFundRevocateSV)obj;

            try
            {
                if (bpObj.Data == null || bpObj.Data.Count == 0)
                    throw new Exception("传入参数为空");

                List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();
                foreach (var doc in bpObj.Data)
                {
                    ConfirmReqFundHead be = ConfirmReqFundHead.Finder.Find($"DocNo='{doc.DocNo}'", null);
                    if (be == null)
                        throw new Exception($"单号：[{doc.DocNo}]查找不到数据");
                    if (be.DocStatus != ReqConfirmDocStatusEnum.Approving)
                        throw new Exception($"单号：[{doc.DocNo}]状态不为提交！");

                    RevocateDoc(be);

                    //处理返回
                    CommonArchiveDataDTOData ret = new CommonArchiveDataDTOData()
                    {
                        ID = be.ID,
                        Code = doc.DocNo,
                    };
                    retlist.Add(ret);
                }
                return ResultUtils.success(bpObj, bpObj.Data, retlist);
            }
            catch (Exception ex)
            {
                return ResultUtils.error(bpObj, bpObj.Data, ex);
            }
        }

        private void RevocateDoc(ConfirmReqFundHead be)
        {
            using(ISession se = Session.Open())
            {
                be.DocStatus = ReqConfirmDocStatusEnum.Opened;

                se.Commit();
            }
        }
    }

	#endregion
	
	
}